package com.liu.sx.util;

import com.liu.sx.entity.Task;
import com.liu.sx.pojo.BW;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;


import java.util.*;

public class BwUtil {


    public  static  boolean getScFromBW(byte [] bw){
         boolean flag =false;
        System.out.println("bw:===="+Arrays.toString(bw));
        String aa = Arrays.toString(bw);
          byte sc = sum(bw,bw.length-2);
          byte sc_result =bw[bw.length-2];
          if(sc==sc_result){
              flag=true;
          }
        return flag;
    }

    /**
     * 方式一：递归，传数组长度
     * @param arr
     * @param n
     * @return
     */
    private static byte sum(byte arr[], int n) {
        if(n == 1) {
            return arr[0];
        }else {
            return (byte) (arr[n-1] + sum(arr, --n));
        }
    }



    public static BW byte2Object(byte[] bw) {
        BW msg = new BW();
        msg.setTag(bw[0]);
        byte[] address_ = Arrays.copyOfRange(bw,1,7);
        System.out.println(Arrays.toString(address_));
        msg.setAddress(BCD2String.bcd2Str(address_));
//        00 00 00 00 003
        msg.setLength(bw[7]);
        msg.setNum(bw[8]);
//    103 时1个灯管状态，104 2个灯管状态
      byte [] aa= Arrays.copyOfRange(bw,9,9+msg.getLength()-1);

      msg.setData(Arrays.copyOfRange(bw,9,9+msg.getLength()-1));


        msg.setSc(bw[bw.length-2]);
        msg.setEnd(bw[bw.length-1]);
        return msg;
    }

    public static byte[] Object2byte(BW bw){
//        ArrayUtils.addAll()
        ByteBuf out = Unpooled.buffer(255);
          out.writeByte(0xfb);
          byte[] address=BCD2String.str2Bcd(bw.getAddress());
//          翻转数组

//         com.liu.sx.util.ArrayUtils.reverseArray(address);
          out.writeBytes(address);
          out.writeByte(bw.getLength());
          out.writeByte(bw.getNum());
          out.writeBytes(bw.getData());

          byte [] sc =  out.readBytes(bw.getLength()+8).array();

          out.resetReaderIndex();
//          核算校验
           int sc_result=  sum(sc,sc.length);


          out.writeByte((byte)sc_result) ;
          out.writeByte(0x16);

          byte [] resposeBw =out.readBytes(bw.getLength()+10).array();

        return resposeBw;
    }

//    public static void main(String[] args) throws ParseException {
//        BW bw = new BW();
//        bw.setTag(0xfb);
//        bw.setAddress("000000000012");
//        bw.setLength(8);
//        bw.setNum(1);
//        byte[] time =BCD2String.time2BCD();
//        byte[] command ={0x04};
//
//        bw.setData( ArrayUtils.addAll(command,time));
//        bw.setSc((byte) 0x12);
//        bw.setEnd((byte) 0x16);
//        Object2byte(bw);
//
//
//        String sbrq="20190917";
//        SimpleDateFormat df  = new SimpleDateFormat("yyyyMMdd");
//
//        Date date = df.parse(sbrq);
//        System.out.println(date.getTime());
//
//        System.out.println(new Date(date.getTime()+900000l));
//    }


    public static BW task2BW(Task task) {
             BW bw = new BW();
        ByteBuf buf = Unpooled.buffer(200);
        if(task.getCommand()== 0x02 ){
//       ip 4 个字节   port  2个字节


           String server = task.getData();
           String [] ip_port =server.split(":");
           String [] ip =ip_port[0].split("\\.");
            buf.writeByte(0x02);
            for (int i=0;i<ip.length;i++) {
                buf.writeByte(Integer.parseInt(ip[i]));
            }
            String port= ArrayUtils.add0ForLength(ip_port[1],4);
            buf.writeBytes(BCD2String.str2Bcd(port));
            int writ =buf.writerIndex();

            byte [] cc =buf.readBytes(writ).array();
            bw.setTag(251);
            bw.setAddress(task.getAddress());
            bw.setLength(8);
            bw.setNum(1);
            bw.setData(cc);
            bw.setSc((byte) 10);
            bw.setEnd((byte) 0x16);

        }



      return bw;

    }





    public static void main(String[] args) {
      //int [] aa = [FB, 00 ,00 ,00 ,00 ,00 12 68 01 22 19 09 19 11 21 22 12 33 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 6F 79 00 00  55 16]


    }
}
